package com.cn.medicalcare.controller.backstageservlet.product;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.Page;
import com.cn.medicalcare.pojo.ProductInfo;
import com.cn.medicalcare.service.ProductInfoService;
import com.cn.medicalcare.service.serviceimpl.ProductInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/product/insertproductinfo.json")
public class ProductInfoInsertServlet extends HttpServlet {

    private ProductInfoService productInfoService = ProductInfoServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String product_id = req.getParameter("product_id");
        String price = req.getParameter("price");
        String listprice = req.getParameter("listprice");
        String description = req.getParameter("description");
        String sendinfo = req.getParameter("sendinfo");
        String cqcinfo = req.getParameter("cqcinfo");
        String payment = req.getParameter("payment");
        String packageinfo = req.getParameter("packageinfo");
        String survival = req.getParameter("survival");
        ProductInfo productInfo = new ProductInfo();
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            productInfo.setProduct_id(Integer.valueOf(product_id));
            productInfo.setPrice(Integer.valueOf(price));
            productInfo.setListprice(Integer.valueOf(listprice));
            productInfo.setPayment(Integer.valueOf(payment));
            productInfo.setSurvival(Integer.valueOf(survival));
            productInfo.setDescription(description).setSendinfo(sendinfo).setCqcinfo(cqcinfo).setPackageinfo(packageinfo);
            if (productInfoService.insertProductInfo(productInfo)) {
                map.put("status", 200);
            } else {
                map.put("status", 500);
            }
        } catch (Exception e) {
            map.put("status", 400);
        }
        resp.getWriter().print(JsonTool.toJson(map));

    }
}
